<!DOCTYPE html>
<html lang="zh-CN" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head th:include="/common/inc::header"/>
<body>
<form class="layui-form" style="margin-top: 15px;margin-left: 30px">
    <div class="layui-form-item">
        <button class="layui-btn" lay-submit lay-filter="addBtn" id="addBtn">新增分享图片</button>
    </div>
</form>
<table class="layui-table" id="grid" lay-filter="grid">
</table>
<script th:replace="/common/inc::incJs"></script>
<style type="text/css">
    .layui-table-cell {
        height: auto !important;
        white-space: normal;
    }
</style>
<script>
    var tableIns;

    $(function () {
        tableIns = createTable({
            url: 'list',
            cols: [[{field: 'id', title: '编号', width: 120},
                {
                    field: 'photoUrl',
                    title: '图片',
                    width: 300,
                    algin: "center",
                    templet: "#imgtmp"
                }, {
                    field: 'status',
                    title: '状态',
                    width: 100,
                    templet: '#statusTpl'
                },{
                    title: '操作',
                    width: 200,
                    align: 'center',
                    toolbar: '#opbar'
                }
            ]]
        })
    });

    layui.table.on('tool(grid)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值
        var tr = obj.tr; //获得当前行 tr 的DOM对象02\
        if (layEvent === 'del') { //删除
            delRecord("/backend/security/sharePhoto/del", obj)
        } else if (layEvent === 'change') { //删除
            changeStatus(data.id, data.status);
        } else if (layEvent === 'edit') { //编辑
            openDialog({
                url: "/backend/security/sharePhoto/detail?id=" + data.id,
                area: ["50%", "65%"],
                btn: ["保存", "取消"]
            });
        }
    });

    //变更标签状态
    function changeStatus(id, status) {
        if (status == "0") {
            $.post('/backend/security/sharePhoto/update', {id: id, status: 1}, function (res) {
                    if (res.code < 0) {
                        layer.msg(res.message);
                    }
                    else {
                        layer.msg("下架成功");
                        tableIns.reload();
                    }
                }
            )
        } else if (status = "1") {
            $.post('/backend/security/sharePhoto/update', {id: id, status: 0}, function (res) {
                    if (res.code < 0) {
                        layer.msg(res.message);
                    }
                    else {
                        layer.msg("上架成功");
                        tableIns.reload();
                    }
                }
            )
        }
    }

    layui.form.on('submit(addBtn)', function () {
        openDialog({
            url: "/backend/security/sharePhoto/detail",
            btn: ["保存", "取消"],
            area: ["50%", "65%"]
        });
        return false;
    });

    layui.form.on('submit(searchBtn)', function (data) {
        tableIns.reload({
            where: data.field
        });
        return false;
    });
</script>
<script type="text/html" id="statusTpl">
    {{#  if(d.status ===1){ }}
    <span style="color: #FF0000;">下架</span>
    {{#  } else { }}
    <span style="color: green;">上架</span>
    {{#  } }}
</script>
<script type="text/html" id="imgtmp">
    <img src="{{d.photoUrl}}" style="width: 150px;height: 150px">
</script>
<script type="text/html" id="opbar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="change">变更状态</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
</body>
</html>
